<template>
	<view>
		<view @click="handlePay">
			<slot></slot>
		</view>
	</view>
</template>

<script>
	/**
	 * 	@paymentType 支付类型 微信支付wxpay / 支付宝支付alipay
	 * 	@orderParams 下订单参数
	 * 	@orderType 下订单方式  default 默认下订单/ quick 快捷消费下订单
	 *  @isContinuesPay 是否继续支付 false下订单  true继续支付
	 *  @payUrl 继续支付url
	 * 	@payParams 支付参数
	 * 	@handlePaySuccess 支付成功函数
	 *  @handlePayFail 支付失败函数
	 * */
	import handlerHttpError from '@/utils/error';
	import http from '@/utils/http';
	import api from '@/api';
	import {
		mapState
	} from 'vuex';
	import {
		throttle
	} from '@/utils/index.js';
	export default {
		name: 'Pay',
		props: {
			paymentType: {
				type: String,
				default: 'wxpay'
			},
			orderParams: {
				type: Object,
				default: () => {
					return {};
				}
			},
			orderType: {
				type: String,
				default: 'default'
			},
			isContinuesPay: {
				type: Boolean,
				default: false
			},
			payUrl: {
				type: String,
				default: ''
			},
			payParams: {
				type: Object,
				default: () => {
					return {};
				}
			}
		},
		data() {
			return {
				orderInfo: {}
			};
		},
		computed: {
			...mapState(['config', 'LoginType'])
		},
		created() {},
		methods: {
			// 获取支付通道 微信支付 支付宝支付 等
			handlePay: throttle(function() {
				uni.showLoading()
				let self = this
				uni.getProvider({
					service: 'payment',
					success: res => {
						const iapChannel = res.provider.find(item => {
							return item == 'wxpay'
						})
						if (iapChannel == 'wxpay') {
							// 第一次支付需要下订单
							if (!self.isContinuesPay) {
								// 区分个人下订单还是企业下订单  LoginType 1企业 2 个人
								if (self.LoginType == 1) {
									// 企业下订单
									if (self.orderType == 'default') {
										// 企业正常下订单
										uni.login({
											success(resCode) {
												self.orderParams.code = resCode.code
												http.post(api.company_pay, self.orderParams)
													.then(resData => {
														let res = resData.data
														if (res.code == 200) {
															if (res.data.pay_status == 1) {
																uni.showToast({
																	title: '支付成功',
																	icon: 'none'
																});
																uni.hideLoading()
																self.$emit(
																	'handlePaySuccess',
																	res);
															} else {
																// 获取订单信息调起支付
																self.pay(res.data
																	.parameter);
															}
														}
													});
											}
										})
									} else if (self.orderType == 'quick') {
										// 企业快捷支付
										uni.login({
											success(resCode) {
												self.orderParams.code = resCode.code
												http.post(api.company_pay_direct_service, self
														.orderParams)
													.then(res => {
														// 获取订单信息调起支付
														if (res.code == 200) {
															if (res.data.pay_status == 1) {
																uni.showToast({
																	title: '支付成功',
																	icon: 'none'
																});
																uni.hideLoading()
																self.$emit(
																	'handlePaySuccess',
																	res);
															} else {
																// 获取订单信息调起支付
																self.pay(res.data
																	.parameter);
															}
														}
													});
											}
										})
									}
								} else if (self.LoginType == 2) {
									uni.login({
										success(resCode) {
											self.orderParams.code = resCode.code
											http.post(api.personal_pay, self.orderParams).then(
												res => {
													if (res.data.code == 200) {
														console.log(res, '200')
														if (res.data.data.pay_status ==
															1) {
															uni.showToast({
																title: '支付成功',
																icon: 'none'
															});
															uni.hideLoading()
															self.$emit('handlePaySuccess',
																res);
														} else {
															// 获取订单信息调起支付
															self.pay(res.data.data
																.parameter);
														}
													}
												});
										}
									})
								}
							} else {
								// 继续支付
								//通过订单id 获取对应的支付参数然后调起支付
								if (self.LoginType == 1) {
									uni.login({
										success(resCode) {
											http.post(api.company_repay, {
												id: self.payParams.id,
												payment: self.paymentType,
												code: resCode.code
											}).then(res => {
												if (res.code == 200) {
													if (res.data.pay_status == 1) {
														uni.showToast({
															title: '支付成功',
															icon: 'none'
														});
														uni.hideLoading()
														self.$emit('handlePaySuccess',
															res);
													} else {
														// 获取订单信息调起支付
														self.pay(res.data.parameter);
													}
												}
											});
										}
									})
								} else if (self.LoginType == 2) {
									uni.login({
										success(resCode) {
											http.post(api.personal_repay, {
												id: self.payParams.id,
												payment: self.paymentType,
												code: resCode.code
											}).then(res => {
												if (res.code == 200) {
													if (res.data.pay_status == 1) {
														uni.showToast({
															title: '支付成功',
															icon: 'none'
														});
														uni.hideLoading()
														self.$emit('handlePaySuccess',
															res);
													} else {
														// 获取订单信息调起支付
														self.pay(res.data.parameter);
													}
												}
											});
										}
									})
								}
								// this.pay(this.payParams)
							}
						} else {
							uni.showToast({
								title: '调起支付失败，请查看配置',
								icon: 'none'
							});
							uni.hideLoading()
						}
					}
				});
			}, 800),
			// 支付
			pay(payData) {
				let parameters = payData.jsApiParameters
				// 调起支付
				uni.requestPayment({
					provider: this.paymentType,
					timeStamp: parameters.timeStamp,
					signType: parameters.signType,
					paySign: parameters.paySign,
					package: parameters.package,
					nonceStr: parameters.nonceStr,
					appId: parameters.appId,
					success: res => {
						uni.hideLoading();
						this.$emit('handlePaySuccess', res);
					},
					fail: err => {
						uni.hideLoading();
						this.$emit('handlePayFail', err);
					}
				});
			}
		}
	};
</script>

<style lang="scss" scoped></style>